<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>GrPPI: worker_pool.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.svg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">GrPPI
   &#160;<span id="projectnumber">0.3.1</span>
   </div>
   <div id="projectbrief">Generic and Reusable Parallel Pattern Interface</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d44c64559bbebec7f509842c48db8b23.html">include</a></li><li class="navelem"><a class="el" href="dir_b519bdaee6a7f8dbbe55c1237cf8f544.html">native</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">worker_pool.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="worker__pool_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="preprocessor">#ifndef GRPPI_NATIVE_WORKER_POOL_H</span></div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="preprocessor">#define GRPPI_NATIVE_WORKER_POOL_H</span></div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;<span class="preprocessor">#include &lt;thread&gt;</span></div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespacegrppi.html">grppi</a> {</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;</div><div class="line"><a name="l00034"></a><span class="lineno"><a class="line" href="classgrppi_1_1worker__pool.html">   34</a></span>&#160;<span class="keyword">class </span><a class="code" href="classgrppi_1_1worker__pool.html">worker_pool</a> {</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;  <span class="keyword">public</span>:</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;</div><div class="line"><a name="l00041"></a><span class="lineno"><a class="line" href="classgrppi_1_1worker__pool.html#ac3ebceb29ce8143d5a8741a029657e72">   41</a></span>&#160;    <a class="code" href="classgrppi_1_1worker__pool.html#ac3ebceb29ce8143d5a8741a029657e72">worker_pool</a>(<span class="keywordtype">int</span> num_threads) noexcept : num_threads_{num_threads} {}</div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;</div><div class="line"><a name="l00047"></a><span class="lineno"><a class="line" href="classgrppi_1_1worker__pool.html#a92f880356d613af8d0270934a4cbaa33">   47</a></span>&#160;    <a class="code" href="classgrppi_1_1worker__pool.html#a92f880356d613af8d0270934a4cbaa33">~worker_pool</a>() noexcept { this-&gt;<a class="code" href="classgrppi_1_1worker__pool.html#afcc57d84e91af6655791c5b013d21d86">wait</a>(); }</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;    <a class="code" href="classgrppi_1_1worker__pool.html#ac3ebceb29ce8143d5a8741a029657e72">worker_pool</a>(<a class="code" href="classgrppi_1_1worker__pool.html">worker_pool</a> &amp;&amp;) noexcept = <span class="keywordflow">default</span>;</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;    <a class="code" href="classgrppi_1_1worker__pool.html">worker_pool</a> &amp; <a class="code" href="classgrppi_1_1worker__pool.html#afc5a6f1d2762e5889d2abcdcb8aaf271">operator=</a>(<a class="code" href="classgrppi_1_1worker__pool.html">worker_pool</a> &amp;&amp;) noexcept = <span class="keywordflow">default</span>;</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;    </div><div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;    <span class="keyword">template</span> &lt;<span class="keyword">typename</span> E, <span class="keyword">typename</span> F, <span class="keyword">typename</span> ... Args&gt;</div><div class="line"><a name="l00062"></a><span class="lineno"><a class="line" href="classgrppi_1_1worker__pool.html#ad7d75e2868bd64d6626731b787cc1abf">   62</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="classgrppi_1_1worker__pool.html#ad7d75e2868bd64d6626731b787cc1abf">launch</a>(<span class="keyword">const</span> E &amp; ex, F f, Args &amp;&amp; ... args) {</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;      workers_.emplace_back([=,&amp;ex]() {</div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;        <span class="keyword">auto</span> manager = ex.thread_manager();</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;        f(args...);</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;      });</div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;    }</div><div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;</div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;    <span class="keyword">template</span> &lt;<span class="keyword">typename</span> E, <span class="keyword">typename</span> F, <span class="keyword">typename</span> ... Args&gt;</div><div class="line"><a name="l00070"></a><span class="lineno"><a class="line" href="classgrppi_1_1worker__pool.html#a04e01c4ec5458a040d6d4e1423b09517">   70</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="classgrppi_1_1worker__pool.html#a04e01c4ec5458a040d6d4e1423b09517">launch_tasks</a>(<span class="keyword">const</span> E &amp; ex, F &amp;&amp; f, Args &amp;&amp; ... args) {</div><div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;      <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;num_threads_; ++i) {</div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;        workers_.emplace_back([=,&amp;ex]() {</div><div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;          <span class="keyword">auto</span> manager = ex.thread_manager();</div><div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;          f(args...);</div><div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;        });</div><div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;      }</div><div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;    }</div><div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;</div><div class="line"><a name="l00083"></a><span class="lineno"><a class="line" href="classgrppi_1_1worker__pool.html#afcc57d84e91af6655791c5b013d21d86">   83</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="classgrppi_1_1worker__pool.html#afcc57d84e91af6655791c5b013d21d86">wait</a>() noexcept {</div><div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;      <span class="keywordflow">for</span> (<span class="keyword">auto</span> &amp;&amp; w : workers_) { w.join(); }</div><div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;      workers_.clear();</div><div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;    }</div><div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;</div><div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;  <span class="keyword">private</span>:</div><div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;    <span class="keyword">const</span> <span class="keywordtype">int</span> num_threads_;</div><div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;    std::vector&lt;std::thread&gt; workers_;</div><div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;};</div><div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;</div><div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;}</div><div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;</div><div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;<span class="preprocessor">#endif</span></div><div class="ttc" id="namespacegrppi_html"><div class="ttname"><a href="namespacegrppi.html">grppi</a></div><div class="ttdef"><b>Definition:</b> callable_traits.h:26</div></div>
<div class="ttc" id="classgrppi_1_1worker__pool_html_ad7d75e2868bd64d6626731b787cc1abf"><div class="ttname"><a href="classgrppi_1_1worker__pool.html#ad7d75e2868bd64d6626731b787cc1abf">grppi::worker_pool::launch</a></div><div class="ttdeci">void launch(const E &amp;ex, F f, Args &amp;&amp;...args)</div><div class="ttdoc">Launch a function in the pool. </div><div class="ttdef"><b>Definition:</b> worker_pool.h:62</div></div>
<div class="ttc" id="classgrppi_1_1worker__pool_html_afcc57d84e91af6655791c5b013d21d86"><div class="ttname"><a href="classgrppi_1_1worker__pool.html#afcc57d84e91af6655791c5b013d21d86">grppi::worker_pool::wait</a></div><div class="ttdeci">void wait() noexcept</div><div class="ttdoc">Wait until all launched tasks have been completed. </div><div class="ttdef"><b>Definition:</b> worker_pool.h:83</div></div>
<div class="ttc" id="classgrppi_1_1worker__pool_html_afc5a6f1d2762e5889d2abcdcb8aaf271"><div class="ttname"><a href="classgrppi_1_1worker__pool.html#afc5a6f1d2762e5889d2abcdcb8aaf271">grppi::worker_pool::operator=</a></div><div class="ttdeci">worker_pool &amp; operator=(worker_pool &amp;&amp;) noexcept=default</div></div>
<div class="ttc" id="classgrppi_1_1worker__pool_html"><div class="ttname"><a href="classgrppi_1_1worker__pool.html">grppi::worker_pool</a></div><div class="ttdoc">Pool of worker threads. This class offers a simple pool of worker threads. </div><div class="ttdef"><b>Definition:</b> worker_pool.h:34</div></div>
<div class="ttc" id="classgrppi_1_1worker__pool_html_ac3ebceb29ce8143d5a8741a029657e72"><div class="ttname"><a href="classgrppi_1_1worker__pool.html#ac3ebceb29ce8143d5a8741a029657e72">grppi::worker_pool::worker_pool</a></div><div class="ttdeci">worker_pool(int num_threads) noexcept</div><div class="ttdoc">Creates a worker pool with a number of threads. </div><div class="ttdef"><b>Definition:</b> worker_pool.h:41</div></div>
<div class="ttc" id="classgrppi_1_1worker__pool_html_a04e01c4ec5458a040d6d4e1423b09517"><div class="ttname"><a href="classgrppi_1_1worker__pool.html#a04e01c4ec5458a040d6d4e1423b09517">grppi::worker_pool::launch_tasks</a></div><div class="ttdeci">void launch_tasks(const E &amp;ex, F &amp;&amp;f, Args &amp;&amp;...args)</div><div class="ttdef"><b>Definition:</b> worker_pool.h:70</div></div>
<div class="ttc" id="classgrppi_1_1worker__pool_html_a92f880356d613af8d0270934a4cbaa33"><div class="ttname"><a href="classgrppi_1_1worker__pool.html#a92f880356d613af8d0270934a4cbaa33">grppi::worker_pool::~worker_pool</a></div><div class="ttdeci">~worker_pool() noexcept</div><div class="ttdoc">Destructs the worker pool after joining with all threads in the pool. </div><div class="ttdef"><b>Definition:</b> worker_pool.h:47</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
</html>
